package org.csu.mypetstore.service;

import org.csu.mypetstore.domain.Cart;
import org.csu.mypetstore.domain.Item;
import org.csu.mypetstore.domain.Product;
import org.csu.mypetstore.persistence.CartMapper;
import org.csu.mypetstore.persistence.ItemMapper;
import org.csu.mypetstore.persistence.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CartService {

    @Autowired
    CartMapper cartMapper;
    @Autowired
    ItemMapper itemMapper;
    @Autowired
    ProductMapper productMapper;

    public Cart getCart(String username){
        Cart cart = new Cart();
        List<Item> itemList = cartMapper.getCart(username);
        boolean InStock = false;
        for(int i=0;i<itemList.size();i++){
            if(itemMapper.getInventoryQuantity(itemList.get(i).getItemId())>=1){
                InStock=true;
            }
            Item item = itemList.get(i);
            Product product = productMapper.getProduct(itemMapper.getItem(item.getItemId()).getProductId());
            item.setProduct(product);
            item.setListPrice(itemMapper.getItem(item.getItemId()).getListPrice());
            cart.addItem(item,InStock,itemList.get(i).getQuantity());
        }
        return cart;
    }

}
